﻿@using Nop.Admin.Models.Logistics
@model LogisticsPlanHistoryModel

@using Telerik.Web.Mvc.UI

@{  
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}

@using (Html.BeginForm())
{
    <table width="100%">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.StartDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.StartDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.EndDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => Model.EndDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CourierId):
            </td>
            <td class="adminData">
                @Html.DropDownList("CourierId", Model.AvailableCouriers)
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" id="search-logistics-history" class="t-button" value="@T("Admin.Common.Search")" />
            </td>
        </tr>
    </table>
    <p>
    </p>
    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<PackageLocationModel>()
                .Name("logistics-history-grid")
                .ClientEvents(events => events.OnDataBinding("logisticsHistoryOnDataBinding"))
                .Columns(columns =>
                {
                    columns.Bound(x => x.PackageId)
                        .Filterable(true)
                        .Centered()
                        .Template(x => Html.ActionLink(x.PackageId.ToString(), "PackageEdit", new { id = x.PackageId }))
                        .ClientTemplate("<a href=\"PackageEdit/<#= PackageId #>\"><#= PackageId #></a>")
                        .Width(35)
                        .Title(T("Admin.Logistics.Common.Fields.PackageId").Text);
                    columns.Bound(x => x.PlanId)
                        .Centered()
                        .Title(T("Admin.Logistics.Common.Fields.PlanId").Text);
                    columns.Bound(x => x.Action)
                        .Title(T("Admin.Logistics.PlannedRoute.Fields.Action").Text);
                    columns.Bound(x => x.ShippingStatusStr)
                        .Title(T("Admin.Logistics.Common.Fields.ShippingStatus").Text);
                    columns.Bound(x => x.AddressZone)
                         .Title(T("Admin.Logistics.Common.Fields.Zone").Text);
                    columns.Bound(x => x.Address.Type)
                        .Title(T("Admin.Logistics.Common.Fields.AddressType").Text);
                    columns.Bound(x => x.Address.FormattedAddress)
                        .Sortable(false)
                        .Title(T("Admin.Logistics.Common.Fields.Address").Text);
                    columns.Bound(x => x.UpdatedOn).Template(x => x.UpdatedOnStr).ClientTemplate("<#= UpdatedOnStr #>");
                    columns.Bound(x => x.Attribute)
                        .Sortable(false)
                        .Title(T("Admin.Logistics.Common.Fields.Attribute").Text);
                })
                .DataBinding(dataBinding => dataBinding.Ajax().Select("ShowPlanHistory", "Logistics"))
                .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                .Sortable()
                .Filterable()
                .Reorderable(reorder => reorder.Columns(true))
                .EnableCustomBinding(true))
            </td>
        </tr>
    </table>
    
    <script type="text/javascript">

        $(document).ready(function () {

            $('#search-logistics-history').click(function () {
                var grid = $('#logistics-history-grid').data('tGrid');
                grid.currentPage = 1; //new search. Set page size to 1
                grid.ajaxRequest();
                return false;
            });

        });

        function logisticsHistoryOnDataBinding(e) {
            var searchModel = {
                StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
                EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
                CourierId: $('#@Html.FieldIdFor(model => model.CourierId)').val()
                };
        e.data = searchModel;
    }

    </script>
    
}